﻿#include "searchbox.h"
#include "ui_searchbox.h"

SearchBox::SearchBox(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SearchBox)
{
    ui->setupUi(this);
}

SearchBox::~SearchBox()
{
    delete ui;
}

void SearchBox::on_btn_go_clicked()
{
    QString cmd=ui->cmd->text().trimmed();
    if(cmd.isEmpty())
        return;

    if(cmd.startsWith(PRE_CMD_HEAD))
    {
        QStringList cmdList=cmd.split(":");
        sendCommand(cmdList.at(0),cmdList.at(1));
    }
    else
    {
        cmd=SEARCH + cmd;
        QDesktopServices::openUrl(QUrl(cmd));
    }
    ui->cmd->clear();
    accept();
}

void SearchBox::sendCommand(const QString &label, const QString &detail)
{
    QByteArray data;
    QDataStream out(&data,QIODevice::WriteOnly);
    out << label << detail ;

    QUdpSocket sender;
    sender.writeDatagram(data,QHostAddress("192.168.1.255"),SEND_PORT);
}
